package net.kesumu.yushabu.storage;

import java.sql.Timestamp;
import java.util.ArrayList;

import net.kesumu.yushabu.storage.YushabuMessageContract.YushabuMessageEntry;
import net.kesumu.yushabu.storage.YushabuSessionContract.YushabuSessionEntry;
import net.kesumu.yushabuserver.entity.YushabuSession;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

public class YushabuSessionDbOperator {
    
    YushabuDbHelper mDbHelper;
    
    public YushabuSessionDbOperator(Context context) {
        mDbHelper = new YushabuDbHelper(context);
    }

    public long insert(YushabuSession yushabuSession) {
        // Gets the data repository in write mode
        SQLiteDatabase db = mDbHelper.getWritableDatabase();

        // Create a new map of values, where column names are the keys
        ContentValues values = new ContentValues();
        values.put(YushabuSessionEntry.COLUMN_NAME_SESSIONID, yushabuSession.getSessionId());
        values.put(YushabuSessionEntry.COLUMN_NAME_FRIENDID, yushabuSession.getFriendId());
        values.put(YushabuSessionEntry.COLUMN_NAME_FRIENDNAME, yushabuSession.getFriendName());

        // Insert the new row, returning the primary key value of the new row
        long newRowId;
        newRowId = db.insert(
                            YushabuSessionEntry.TABLE_NAME,
                            null,
                            values);
        return newRowId;
    }
    
    public YushabuSession selectByFriendIdAndSessionId(String friendId, String sessionId) {
        SQLiteDatabase db = mDbHelper.getReadableDatabase();

        // Define a projection that specifies which columns from the database
        // you will actually use after this query.
        String[] projection = {
                                YushabuSessionEntry._ID,
                                YushabuSessionEntry.COLUMN_NAME_SESSIONID,
                                YushabuSessionEntry.COLUMN_NAME_FRIENDID,
                                YushabuSessionEntry.COLUMN_NAME_FRIENDNAME
                                };
        
        // Define 'where' part of query.
        String selection = YushabuSessionEntry.COLUMN_NAME_FRIENDID + " = ?" +
                            " AND " + YushabuSessionEntry.COLUMN_NAME_SESSIONID + " = ?";
        // Specify arguments in placeholder order.
        String[] selectionArgs = { friendId, sessionId};
   
        // How you want the results sorted in the resulting Cursor
        String sortOrder =
                YushabuSessionEntry.COLUMN_NAME_FRIENDID;
   
        Cursor cursor = db.query(
            YushabuSessionEntry.TABLE_NAME,  // The table to query
            projection,                               // The columns to return
            selection,                                // The columns for the WHERE clause
            selectionArgs,                            // The values for the WHERE clause
            null,                                     // don't group the rows
            null,                                     // don't filter by row groups
            sortOrder                                 // The sort order
            );
        if(cursor.getCount()>0) {
            cursor.moveToFirst();
            YushabuSession yushabuSession = new YushabuSession();
            String itemSessionId = cursor.getString(
                cursor.getColumnIndexOrThrow(YushabuSessionEntry.COLUMN_NAME_SESSIONID));
            String itemFriendId = cursor.getString(
                cursor.getColumnIndexOrThrow(YushabuSessionEntry.COLUMN_NAME_FRIENDID)
            );
            String itemFriendName = cursor.getString(
                    cursor.getColumnIndexOrThrow(YushabuSessionEntry.COLUMN_NAME_FRIENDNAME)
                );
            yushabuSession.setSessionId(Integer.parseInt(itemSessionId));
            yushabuSession.setFriendId(Integer.parseInt(itemFriendId));
            yushabuSession.setFriendName(itemFriendName);
            return yushabuSession;
        }
        return null;
    }
    
    public ArrayList<YushabuSession> selectBySessionId(String sessionId) {
        ArrayList<YushabuSession> yushabuSessions = new ArrayList<YushabuSession>();
        SQLiteDatabase db = mDbHelper.getReadableDatabase();

        // Define a projection that specifies which columns from the database
        // you will actually use after this query.
        String[] projection = {
                                YushabuSessionEntry._ID,
                                YushabuSessionEntry.COLUMN_NAME_SESSIONID,
                                YushabuSessionEntry.COLUMN_NAME_TYPE,
                                YushabuSessionEntry.COLUMN_NAME_CREATEDTIME,
                                YushabuSessionEntry.COLUMN_NAME_NAME,
                                YushabuSessionEntry.COLUMN_NAME_FRIENDID,
                                YushabuSessionEntry.COLUMN_NAME_FRIENDNAME
                                };
   
        
        // Define 'where' part of query.
        String selection = YushabuSessionEntry.COLUMN_NAME_SESSIONID + " = ?";
        // Specify arguments in placeholder order.
        String[] selectionArgs = { sessionId};
        
        // How you want the results sorted in the resulting Cursor
        String sortOrder =
                YushabuSessionEntry._ID;
   
        Cursor cursor = db.query(
            YushabuSessionEntry.TABLE_NAME,  // The table to query
            projection,                               // The columns to return
            selection,                                // The columns for the WHERE clause
            selectionArgs,                            // The values for the WHERE clause
            null,                                     // don't group the rows
            null,                                     // don't filter by row groups
            sortOrder                                 // The sort order
            );
        if(cursor.getCount()>0) {
            cursor.moveToFirst();
            do {
                YushabuSession yushabuSession = new YushabuSession();
                String itemSessionId = cursor.getString(
                    cursor.getColumnIndexOrThrow(YushabuSessionEntry.COLUMN_NAME_SESSIONID));
                String itemName = cursor.getString(
                        cursor.getColumnIndexOrThrow(YushabuSessionEntry.COLUMN_NAME_NAME)
                    );
                String itemFriendId = cursor.getString(
                    cursor.getColumnIndexOrThrow(YushabuSessionEntry.COLUMN_NAME_FRIENDID)
                );
                String itemFriendName = cursor.getString(
                        cursor.getColumnIndexOrThrow(YushabuSessionEntry.COLUMN_NAME_FRIENDNAME)
                    );
                yushabuSession.setSessionId(Integer.parseInt(itemSessionId));
                if(yushabuSession.getSessionId() != 0) {
                    yushabuSession.setName(itemName);
                }
                yushabuSession.setFriendId(Integer.parseInt(itemFriendId));
                yushabuSession.setFriendName(itemFriendName);
                yushabuSessions.add(yushabuSession);
            }while(cursor.moveToNext());
        }
        return yushabuSessions;
    }
    
    public ArrayList<YushabuSession> selectAll(){
        ArrayList<YushabuSession> yushabuSessions = new ArrayList<YushabuSession>();
        SQLiteDatabase db = mDbHelper.getReadableDatabase();

        // Define a projection that specifies which columns from the database
        // you will actually use after this query.
        String[] projection = {
                                YushabuSessionEntry._ID,
                                YushabuSessionEntry.COLUMN_NAME_SESSIONID,
                                YushabuSessionEntry.COLUMN_NAME_TYPE,
                                YushabuSessionEntry.COLUMN_NAME_CREATEDTIME,
                                YushabuSessionEntry.COLUMN_NAME_NAME,
                                YushabuSessionEntry.COLUMN_NAME_FRIENDID,
                                YushabuSessionEntry.COLUMN_NAME_FRIENDNAME
                                };
   
        // How you want the results sorted in the resulting Cursor
        String sortOrder =
                YushabuSessionEntry._ID;
   
        Cursor cursor = db.query(
            YushabuSessionEntry.TABLE_NAME,  // The table to query
            projection,                               // The columns to return
            null,                                // The columns for the WHERE clause
            null,                            // The values for the WHERE clause
            null,                                     // don't group the rows
            null,                                     // don't filter by row groups
            sortOrder                                 // The sort order
            );
        if(cursor.getCount()>0) {
            cursor.moveToFirst();
            do {
                YushabuSession yushabuSession = new YushabuSession();
                String itemSessionId = cursor.getString(
                    cursor.getColumnIndexOrThrow(YushabuSessionEntry.COLUMN_NAME_SESSIONID));
                String itemName = cursor.getString(
                        cursor.getColumnIndexOrThrow(YushabuSessionEntry.COLUMN_NAME_NAME)
                    );
                String itemFriendId = cursor.getString(
                    cursor.getColumnIndexOrThrow(YushabuSessionEntry.COLUMN_NAME_FRIENDID)
                );
                String itemFriendName = cursor.getString(
                        cursor.getColumnIndexOrThrow(YushabuSessionEntry.COLUMN_NAME_FRIENDNAME)
                    );
                yushabuSession.setSessionId(Integer.parseInt(itemSessionId));
                if(yushabuSession.getSessionId() != 0) {
                    yushabuSession.setName(itemName);
                }
                yushabuSession.setFriendId(Integer.parseInt(itemFriendId));
                yushabuSession.setFriendName(itemFriendName);
                yushabuSessions.add(yushabuSession);
            }while(cursor.moveToNext());
        }
        return yushabuSessions;
    }
}
